package com.cqyc.shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqyc.shop.comm.JdbcUtils;
import com.cqyc.shop.comm.Pager;
import com.cqyc.shop.comm.commException;
import com.cqyc.shop.domain.Shop;

public class ShopDao {

	public Shop rowmap(ResultSet rs) throws SQLException {
		Shop shop = new Shop();
		shop.setId(rs.getInt("id"));
		shop.setShopname(rs.getString("shopname"));
		shop.setShopinfo(rs.getString("shopinfo"));
		shop.setShopimg(rs.getString("shopimg"));
		shop.setProducedate(rs.getDate("producedate"));
		shop.setProduce(rs.getString("produce"));
		shop.setPrice(rs.getBigDecimal("price"));
		shop.setShoptotal(rs.getInt("shoptotal"));
		return shop;
	}
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	/**
	 * 商品修改
	 * @param shop
	 */
	public void update(Shop shop) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="update shopinfo set shopname=?,shopinfo=?,shopimg=?,producedate=?,produce=?,price=? ,shoptotal=? where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, shop.getShopname());
			ps.setString(2, shop.getShopinfo());
			ps.setString(3, shop.getShopimg());
			ps.setDate(4,new java.sql.Date(shop.getProducedate().getTime()));
			ps.setString(5, shop.getProduce());
			ps.setBigDecimal(6, shop.getPrice());
			ps.setInt(7, shop.getShoptotal());
			ps.setInt(8,shop.getId());
			int ret = ps.executeUpdate();
			
			if(ret<0) {
				throw new commException("商品修改失败"); 
			}
			logger.debug("修改数据成功");
		} catch (Exception e) {
			logger.error("{}",e.getMessage());
			throw new commException("修改商品失败");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}

	/**
	 * 商品保存
	 * @param shop
	 */
	public void save(Shop shop) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="insert into shopinfo(shopname,shopinfo,shopimg,producedate,produce,price,shoptotal) values(?,?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, shop.getShopname());
			ps.setString(2, shop.getShopinfo());
			ps.setString(3, shop.getShopimg());
			ps.setDate(4,new java.sql.Date(shop.getProducedate().getTime()));
			ps.setString(5, shop.getProduce());
			ps.setBigDecimal(6, shop.getPrice());
			ps.setInt(7, shop.getShoptotal());
			int ret = ps.executeUpdate();
			
			if(ret<0) {
				throw new commException("商品插入失败"); 
			}
			logger.debug("插入数据成功");
		} catch (Exception e) {
			logger.error("{}",e.getMessage());
			throw new commException("插入商品失败");
		}finally {
			JdbcUtils.close(conn, ps, rs);
			
		}

	}

	/**
	 * 商品查询
	 * @param shop
	 * @param pager 
	 * @return 
	 */
	public List<Shop> findPager(Shop shop, Pager pager) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
	
		try {
			conn = JdbcUtils.getConnerction();
			String sql="select * from shopinfo where shopname like ? limit ?,? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1,"%"+shop.getShopname()+"%");
			ps.setInt(2, (pager.getCurrentPage()-1)*pager.getPageSize());
			ps.setInt(3, pager.getPageSize());
			rs = ps.executeQuery();
			List<Shop> shops = new ArrayList<>();
			while(rs.next() ){
					shops.add(rowmap(rs));
			}
			logger.debug("查询数据成功");
			return shops;
		} catch (Exception e) {
			logger.debug("查询失败");
			throw new commException("查询数据失败");
		}
		finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}

	public Shop findId(Integer idd) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
	
		try {
			conn = JdbcUtils.getConnerction();
			String sql="select * from shopinfo where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, idd);
			rs = ps.executeQuery();
			while(rs.next()) {
				return rowmap(rs);
			}
			logger.debug("商品信息已经查询");
		} catch (Exception e) {
			logger.debug("查询失败");
			throw new commException("查询数据失败");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		return null;
		
	}
}